<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>添加/编辑图书</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="90px" ref="editForm">
          <el-row>
            <el-col :span="20">
              <el-form-item label="书名:" prop="name">
                <el-input v-model="editForm.name" auto-complete="off" placeholder="填写书名"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="图书分类:" prop="book_cat_id">
                <div style="display: flex;">
                  <el-select placeholder="图书分类" v-model="first_id" @change="changeSearchSelectFn(1)" class="mr-10">
                    <el-option :label="item.name" :value="index" v-for="(item, index) in tableList"
                               :key="index"></el-option>
                  </el-select>
                  <el-select placeholder="图书分类" @change="changeSearchSelectFn(2)" v-if="sonselectList" v-model="son_id">
                    <el-option :label="item.name" :value="item.id" v-for="(item, index) in sonselectList"
                               :key="index"></el-option>
                  </el-select>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="存放位置:" prop="position">
                <el-input v-model="editForm.position" auto-complete="off" placeholder="填写存放位置"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="状态:" prop="status">
                <el-radio-group v-model="editForm.status">
                  <el-radio :label="1">开启借阅</el-radio>
                  <el-radio :label="0">禁止借阅</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="4">
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="价格:" prop="press">
                <el-input v-model="editForm.press" auto-complete="off" placeholder="填写价格"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="作者:" prop="editor">
                <el-input v-model="editForm.editor" auto-complete="off" placeholder="填写作者"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="!bookDetailId">
            <el-col :span="10">
              <el-form-item label="条形码:" prop="code">
                <el-input v-model="editForm.code" auto-complete="off" placeholder="填写条形码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="数量:" prop="code_number">
                <el-input v-model="editForm.code_number" auto-complete="off" placeholder="填写数量"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="submitBook" size="small" type="primary">确定并保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  props: ['visible', 'bookDetailId'], // commoent 学生点评 collection 学生采集
  components: {},
  data() {
    return {
      dialogVisible: false,
      first_id: '',
      son_id: '',
      tableList: [],
      sonselectList: null,
      editForm: {
        code: '',
        editor: '',
        name: '',
        position: '',
        press: '',
        status: 1,
        num: 0,
        book_cat_id: '',
        code_number: ''
      }, // 编辑界面数据
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if(this.visible){
        this.coteryInitFn()
      }
    }
  },
  created() {

  },
  methods: {
    submitBook () {
      // 判断新增 编辑
      if(!this.editForm.name){
        this.$message.error('填写书名')
        return
      }
      if (this.sonselectList && !this.son_id) {
        this.$message.error('请选择图书分类')
        return
      }
      if(!this.editForm.position){
        this.$message.error('图书存放位置')
        return
      }
      let url_ = this.api.book_add
      let data_ = this.editForm
      if (this.bookDetailId) {
        url_ = this.api.book_edit
        data_.id = this.bookDetailId
      }
      this.$post(url_, data_).then((response) => {
        if (response.code === 0) {
          this.$message.success(response.msg)
          if (!this.bookDetailId) {
            this.$refs['editForm'].resetFields()
            this.editForm.book_cat_id = ''
            this.first_id = ''
            this.son_id = ''
            this.sonselectList = null
          }
          this.$emit('InitFn')
        } else {
          this.$message.error(response.msg)
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    changeSearchSelectFn(option) {
      // option 1 顶级分类 2 二级分类
      if (option === 1) {
        this.son_id = ''
        this.sonselectList = this.tableList[this.first_id].child
        if (JSON.stringify(this.sonselectList) === '{}') {
          this.sonselectList = null
        }
        this.editForm.book_cat_id = this.first_id
      } else if (option === 2) {
        this.editForm.book_cat_id = this.son_id
      }
    },
    coteryInitFn() {
      this.$post(this.api.book_cat, {p: 1}).then((response) => {
        if (response.code === 0) {
          let list_ = response.list
          let obj_son = {'0': {name: '全部', id: 0}}
          for (let i in list_) {
            if (list_[i].tid === 0) {
              console.log('obj_son[list_[i].id].child || {}:', obj_son[list_[i].id])
              // 一级分类
              obj_son[list_[i].id] = obj_son[list_[i].id] || {}
              obj_son[list_[i].id].id = list_[i].id
              obj_son[list_[i].id].name = list_[i].name
              obj_son[list_[i].id].child = obj_son[list_[i].id].child || {}
            } else {
              obj_son[list_[i].tid].child[list_[i].id] = obj_son[list_[i].tid].child[list_[i].id] || {}
              obj_son[list_[i].tid].child[list_[i].id].id = list_[i].id
              obj_son[list_[i].tid].child[list_[i].id].name = list_[i].name
            }
          }
          this.tableList = obj_son // 一级分类目录
          console.log('this.tableList:', this.tableList)
          if (this.bookDetailId) {
            this.InitFn()
          }
        } else {
          console.log('请求失败')
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    InitFn() {
      this.$fetch(this.api.book_edit, {
        id: this.bookDetailId
      }).then((response) => {
        if (response.code === 0) {
          this.editForm.name = response.vo.name
          this.editForm.editor = response.vo.editor
          this.editForm.position = response.vo.position
          this.editForm.press = response.vo.press
          this.editForm.status = response.vo.status
          console.log('this.tableList:', this.tableList)
          if (response.vo.cat.tid === 0) {
            this.first_id = response.vo.cat.id.toString()
            this.editForm.book_cat_id = response.vo.cat.id.toString()
          } else {
            this.first_id = response.vo.cat.tid.toString()
            this.son_id = response.vo.cat.id
            this.editForm.book_cat_id = response.vo.cat.id.toString()
            this.sonselectList = this.tableList[this.first_id].child
          }
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 50%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding:20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

</style>


